# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# File: $Id: jasmine_frame.py 24 2007-12-18 11:48:29Z mingli.yuan $
# Revision: $Revision: 24 $
#----------------------------------------------------------------------------
# py-jasmine: A python GUI tool for pdf journal making
# Copyright (C) 2007  Mingli Yuan
#
# This program is a part of py-jasmine.
#
# py-jasmine is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# py-jasmine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with py-jasmine. If not, see <http://www.gnu.org/licenses/>.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://py-jasmine.googlecode.com/svn/trunk/.
#
#----------------------------------------------------------------------------

"""py-jasmine main frame
@version: $Id: jasmine_frame.py 24 2007-12-18 11:48:29Z mingli.yuan $
@author: Mingli Yuan <me@mingli-yuan.info>
@license: gpl
"""

__version__ = "$Revision: 24 $"


import wx

from jasmine_book import JasmineBook, JasmineIssueBook, JasmineTemplateBook
from dialogs import NewIssueDialog, OpenIssueDialog, NewTemplateDialog, OpenTemplateDialog
from model.template import Template
from model.issue import Issue


ID_New_Issue = wx.NewId()
ID_Open_Issue = wx.NewId()
ID_New_Template = wx.NewId()
ID_Open_Template = wx.NewId()
ID_Seal_Template = wx.NewId()

ID_Import = wx.NewId()
ID_Generate = wx.NewId()
ID_Send = wx.NewId()

ID_Font = wx.NewId()

ID_About = wx.NewId()


class JasmineFrame(wx.Frame):
    def __init__(
            self, parent, ID, title, pos=wx.DefaultPosition,
            size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
        ):

        wx.Frame.__init__(self, parent, ID, title, pos, size, style)

        # Building menu
        self.menu_bar = self.BuildMenu()
        self.SetMenuBar(self.menu_bar)

        # Binding menu actions
        self.BindMenuActions()

        self.book = None

        self.status_bar = wx.StatusBar(self)
        self.SetStatusBar(self.status_bar)

        self.Bind(wx.EVT_MENU, self.OnNewIssue, id=ID_New_Issue)
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)

    def BuildMenu(self):
        menu_bar = wx.MenuBar()

        file_menu = wx.Menu()
        file_menu.Append(ID_New_Issue, "New Issue")
        file_menu.Append(ID_Open_Issue, "Open Issue")
        file_menu.AppendSeparator();
        file_menu.Append(ID_New_Template, "New Template")
        file_menu.Append(ID_Open_Template, "Open Template")
        self.mn_seal = file_menu.Append(ID_Seal_Template, "Seal Template")
        self.mn_seal.Enable(False)
        file_menu.AppendSeparator();
        file_menu.Append(wx.ID_CLOSE, "Close")
        file_menu.Append(wx.ID_EXIT, "Exit")

        actions_menu = wx.Menu()
        actions_menu.Append(ID_Import, "Importing Mails...")
        actions_menu.Append(ID_Generate, "Generating PDF...")
        actions_menu.Append(ID_Send, "Sending New Issue...")

        options_menu = wx.Menu()
        options_menu.Append(ID_Font, "Font Options")

        help_menu = wx.Menu()
        help_menu.Append(ID_About, "About...")

        menu_bar.Append(file_menu, "File")
        menu_bar.Append(actions_menu, "Actions")
        menu_bar.Append(options_menu, "Options")
        menu_bar.Append(help_menu, "Help")

        return menu_bar

    def BindMenuActions(self):
        self.Bind(wx.EVT_MENU, self.OnNewIssue, id=ID_New_Issue)
        self.Bind(wx.EVT_MENU, self.OnOpenIssue, id=ID_Open_Issue)
        self.Bind(wx.EVT_MENU, self.OnNewTemplate, id=ID_New_Template)
        self.Bind(wx.EVT_MENU, self.OnOpenTemplate, id=ID_Open_Template)
        self.Bind(wx.EVT_MENU, self.OnSealTemplate, id=ID_Seal_Template)
        self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_CLOSE)
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)

    def OnNewIssue(self, event):
        dlg = NewIssueDialog(self, "Open File", "", fit=1, size=(300, -1))
        dlg.Center()
        val = dlg.ShowModal()
        if val == wx.ID_OK:
            template_file = dlg.GetTemplate()
            base_path = dlg.GetBasePath()
            if(self.book != None):
                self.book.Close()
            self.book = JasmineIssueBook(self, -1)

        dlg.Destroy()

    def OnOpenIssue(self, event):
        dlg = OpenIssueDialog(self, "Open Directory", "", fit=1, size=(300, -1))
        dlg.Center()
        val = dlg.ShowModal()
        if val == wx.ID_OK:
            base_path = dlg.GetBasePath()
            if(self.book != None):
                self.book.Close()
            self.book = JasmineIssueBook(self, -1)

        dlg.Destroy()

    def OnNewTemplate(self, event):
        dlg = NewTemplateDialog(self, "Open Directory", "", fit=1, size=(300, -1))
        dlg.Center()
        val = dlg.ShowModal()
        if val == wx.ID_OK:
            base_path = dlg.GetBasePath()
            if(self.book != None):
                self.book.Close()
            self.model = Template(base_path)
            self.book = JasmineTemplateBook(self, -1, model=self.model)
            self.mn_seal.Enable(True)

        dlg.Destroy()

    def OnOpenTemplate(self, event):
        dlg = OpenTemplateDialog(self, "Open Directory", "", fit=1, size=(300, -1))
        dlg.Center()
        val = dlg.ShowModal()
        if val == wx.ID_OK:
            base_path = dlg.GetTemplate()
            if(self.book != None):
                self.book.Close()
            self.model = Template(base_path)
            self.book = JasmineTemplateBook(self, -1, model=self.model)
            self.mn_seal.Enable(True)

        dlg.Destroy()

    def OnSealTemplate(self, event):
        if(self.model.meta == "template"):
            self.model.seal()

    def OnClose(self, event):
        if(self.book != None):
            self.book.Close()
        self.book = None
        self.mn_seal.Enable(False)

    def OnExit(self, event):
        self.Close()

